package org.itboys.mobile.service.mongo.mobile;

import com.google.common.collect.Maps;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.entity.mongo.SundayCollection;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *作者：zzq
 * 时间：2017年5月10日
 * 描述：花果山（农厨）_移动端_收藏_service
 */
@Service
public class SundayMobileCollectionService extends BaseMongoService<SundayCollection> {

    @Autowired
    private ResourceHolder resourceHolder;




    @Resource(name = "mobileDs")
    private MongoDataSource sundayDataSource;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return sundayDataSource;
    }

    @Override
    protected Class<SundayCollection> getEntityClass() {
        return SundayCollection.class;
    }

    /**
     * 新增收藏  或者取消收藏
     * @param memberId
     * @param type
     * @param otherId
     */
    public void collection(Long memberId,Integer type,Long otherId){
            Map<String, Object> param = Maps.newHashMapWithExpectedSize(4);
            param.clear();
            param.put("memberId",memberId);
            param.put("type",type);
            param.put("otherId",otherId);

            List<SundayCollection> collections=super.list(param);
            if(ListUtil.isNull(collections)){
                super.save(new SundayCollection(memberId,type,otherId));
            }else {

                super.doDelete(param);
            }
    }

/**
     * 获取被收藏的对象ID集合
     * @param memberId
     * @param type
     * @return
     */
    public List<Long> getCollectionIds( Long memberId, Integer type){
        List<Map<String,Object>> results=new ArrayList<Map<String,Object>>();
        Map<String ,Object> param= new HashMap<String,Object>();

        param.put("memberId",memberId);
        param.put("type",type);
        List<SundayCollection> collections=super.list(param);
        List<Long> otherIds=new ArrayList<Long>();

        if(ListUtil.isNotNull(collections)){
            for(SundayCollection collection:collections){
                otherIds.add(collection.getOtherId());
            }
        }

        return otherIds;
    }

    /**
     * 判断用户是否收藏。 1商品，2菜谱 3，资讯
     * @param memberId
     * @param type
     * @return
     */
    public Integer checkIsCollection(Long memberId,Integer type,Long otherId){

        Map<String,Object> param=new HashMap<String,Object>();
        param.put("memberId",memberId);
        param.put("type",type);
        param.put("otherId",otherId);
        List<SundayCollection> collections=super.list(param);
        if(ListUtil.isNotNull(collections)){
            return 1;
        }
        return 0;
    }

}
